
#define _WIN32_WINNT 0x0500
#include "Win32CriticalSections.h"

using namespace ttg::threads::win32;

//--------------------------------
//--- Win32FastCriticalSection ---
//--------------------------------

Win32FastCriticalSection::Win32FastCriticalSection()
{
	this->flag = 0;
}

bool Win32FastCriticalSection::tryEnter()
{
	return InterlockedExchange(&flag, 1) == 0;
}

void Win32FastCriticalSection::enter()
{
	while (InterlockedExchange(&flag, 1))
		Sleep(0);
}

void Win32FastCriticalSection::leave()
{
	InterlockedExchange(&flag, 0);
}

//--------------------------------
//--- Win32SlowCriticalSection ---
//--------------------------------

Win32SlowCriticalSection::Win32SlowCriticalSection()
{
	InitializeCriticalSection(&cs);
}

bool Win32SlowCriticalSection::tryEnter()
{
	return TryEnterCriticalSection(&cs) != 0;
}

void Win32SlowCriticalSection::enter()
{
	EnterCriticalSection(&cs);
}

void Win32SlowCriticalSection::leave()
{
	LeaveCriticalSection(&cs);
}

Win32SlowCriticalSection::~Win32SlowCriticalSection()
{
	DeleteCriticalSection(&cs);
}
